package com.example.boot1;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

//做加法，和countDownLatch相反
public class CyclicBarrierDemo {
    //集到了7个龙珠，召唤神龙

    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,() -> {
            System.out.println("*********召唤神龙");
        });
        for (int i = 1; i <= 7 ; i++) {
            final int tempInt = i;
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName()+"\t 收集到第"+tempInt+"颗龙珠");
                try {
                    //开会，来早了就等着；等到齐了之后就会执行上面一行的system.out.println
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            }, String.valueOf(i)).start();
        }
    }
}
